package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Goods;
import com.jxc.model.po.Transferlist;
import com.jxc.model.po.TransferlistItem;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VTransferlistItem;
import com.jxc.service.TransferlistItemServiceI;

@Service("transferlistItemService")
public class TransferlistItemServiceImpl implements TransferlistItemServiceI {

	@Autowired
	private BaseDaoI<TransferlistItem> transferlistItemDao;

	@Override
	public List<VTransferlistItem> combobox(VTransferlistItem vtli) {
		return this.changeModel(this.find(vtli));
	}

	@Override
	public DataGrid datagrid(VTransferlistItem vtli) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vtli)));
		dg.setTotal(this.total(vtli));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					TransferlistItem tl = transferlistItemDao.get(TransferlistItem.class, Long.parseLong(id.trim()));
					if (tl != null) {
						transferlistItemDao.delete(tl);
					}
				}
			}
		}
	}

	@Override
	public void save(VTransferlistItem vtli) {
		TransferlistItem tli = new TransferlistItem();
		BeanUtils.copyProperties(vtli, tli);
		if (vtli.getGid() != null) {
			tli.setGoods(new Goods(vtli.getGid()));
		}
		if (vtli.getTlid() != null) {
			tli.setTransferlist(new Transferlist(vtli.getTlid()));
		}
		this.transferlistItemDao.save(tli);
	}

	@Override
	public void update(VTransferlistItem vtli) {
		TransferlistItem tli = transferlistItemDao.get(TransferlistItem.class, vtli.getId());
		BeanUtils.copyProperties(vtli, tli, new String[] { "id" });
		if (vtli.getGid() != null) {
			tli.setGoods(new Goods(vtli.getGid()));
		}
		if (vtli.getTlid() != null) {
			tli.setTransferlist(new Transferlist(vtli.getTlid()));
		}
		this.transferlistItemDao.update(tli);
	}

	private List<VTransferlistItem> changeModel(List<TransferlistItem> tlis) {
		List<VTransferlistItem> vtlis = new ArrayList<VTransferlistItem>();
		if (tlis != null && tlis.size() > 0) {
			for (TransferlistItem tli : tlis) {
				VTransferlistItem vtli = new VTransferlistItem();
				BeanUtils.copyProperties(tli, vtli);
				if (tli.getGoods() != null) {
					Goods goods = tli.getGoods();
					vtli.setGid(goods.getId());
				}
				if (tli.getTransferlist() != null) {
					Transferlist tl = tli.getTransferlist();
					vtli.setTlid(tl.getId());
				}
				vtlis.add(vtli);
			}
		}
		return vtlis;
	}

	private List<TransferlistItem> find(VTransferlistItem vtli) {
		String hql = "from TransferlistItem t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vtli, hql, values);

		if (vtli.getSort() != null && vtli.getOrder() != null) {
			hql += " order by " + vtli.getSort() + " " + vtli.getOrder();
		}
		return transferlistItemDao.find(hql, values, vtli.getPage(), vtli.getRows());
	}

	private Long total(VTransferlistItem vtli) {
		String hql = "select count(*) from TransferlistItem t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vtli, hql, values);
		return transferlistItemDao.count(hql, values);
	}

	private String addWhere(VTransferlistItem vtli, String hql, List<Object> values) {
		if (vtli.getGid() != null) {
			hql += " and t.goods.id = ? ";
			values.add(vtli.getGid());
		}

		if (vtli.getTlid() != null) {
			hql += " and t.transferlist.id = ? ";
			values.add(vtli.getTlid());
		}

		return hql;
	}

	@Override
	public void batchSave(List<VTransferlistItem> vtlis, Long tlid) {
		for (VTransferlistItem vtli : vtlis) {
			vtli.setTlid(tlid);
			this.save(vtli);
		}
	}

}
